<?php
require_once (dirname(__FILE__)."/../config/config.php");
$method = $_REQUEST['method'];
if($method=='addCustomer')
{
	$flag=true;
	$event_user_id=$_REQUEST['event_user_id'];
	$event_id=$_REQUEST['event_id'];
	$partner_id=$_REQUEST['partner_id'];
	$event_code=$_REQUEST['event_code'];
	$firstname=$_REQUEST['firstname'];
	$lastname=$_REQUEST['lastname'];
	$emailaddress=$_REQUEST['emailaddress'];
	$postcode=$_REQUEST['postcode'];
	$mobile=$_REQUEST['mobile'];
	$ip=$_REQUEST['ip'];
	$lang=$_REQUEST['lang'];
	$step=$_REQUEST['step'];
    $award_address_id=$_REQUEST['award_address_id'];	
	if($event_user_id==""){
		$sqluserinsert="insert into zy_event_customer(event_id,partner_id,event_code,firstname,lastname,emailaddress,postcode,mobile,ip,lang,step,award_address_id) value ('$event_id','$partner_id','$event_code','$firstname','$lastname','$emailaddress','$postcode','$mobile','$ip','$lang','$step','$award_address_id')";
		mysql_query($sqluserinsert);
		$event_customer_id=mysql_insert_id();
	}else{
		$selectsql="select * from zy_event_customer where customer_id=$event_user_id";
		$rs=mysql_query($selectsql);
		while($row=mysql_fetch_array($rs))
		{
			$event_id=empty($event_id)?$row['event_id']:$event_id;
			$partner_id=empty($partner_id)?$row['partner_id']:$partner_id;
			$event_code=empty($event_code)?$row['event_code']:$event_code;
			$firstname=empty($firstname)?$row['firstname']:$firstname;
			$lastname=empty($lastname)?$row['lastname']:$lastname;
			$emailaddress=empty($emailaddress)?$row['emailaddress']:$emailaddress;
			$postcode=empty($postcode)?$row['postcode']:$postcode;
			$mobile=empty($mobile)?$row['mobile']:$mobile;
			$ip=empty($ip)?$row['ip']:$ip;
			$lang=empty($lang)?$row['lang']:$lang;
			$step=empty($step)?$row['step']:$step;
			$award_address_id=empty($award_address_id)?$row['award_address_id']:$award_address_id;
		}
		$sqlupdateuser="update zy_event_customer set event_id='$event_id',partner_id='$partner_id',event_code='$event_code',firstname='$firstname',lastname='$lastname',emailaddress='$emailaddress',postcode='$postcode',mobile='$mobile',ip='$ip',lang='$lang',step='$step',award_address_id='$award_address_id' where customer_id=$event_user_id";
		mysql_query($sqlupdateuser);
		$event_customer_id=$event_user_id;
	}
	
		$event_user_ex=$_REQUEST["event_user_extension"];
		if(strstr($event_user_ex,"\\")==true)
		{
		$str_ex=str_replace("\\","",$event_user_ex);
		}else{
		$str_ex=$event_user_ex;
		}
		$event_customer_ex=unserialize($str_ex);
     	if(count($event_customer_ex)==0)
     	{
			$flag=false;
     	}else{
			$flag=true;
     	}
		if($flag==true)
		{
		foreach ($event_customer_ex as $k => $userexval ) {
     		$sqluserexinsert="insert into zy_event_customer_extension(event_customer_id,event_customer_extension_cn,event_customer_extension_en,event_customer_extension_content) value ($event_customer_id,'','$k','$userexval')";
     		mysql_query($sqluserexinsert);
		}
	}
	
		$step=$_REQUEST['step']+1;
     	
     	$sqlcount="select * from zy_event_step where event_id=".$_REQUEST['event_id'];
     	$rs1=mysql_query($sqlcount);
     	$rownum=mysql_num_rows($rs1);
     	$url="http://prepaidselect.com/demo1/surveys1.php?event_user_id=".base64_encode($event_customer_id);
     	$email_to=$_REQUEST['emailaddress'];
     	$first_name=$_REQUEST['firstname'];
    	$mobile=$_REQUEST['mobile'];
     	$lang=$_REQUEST['lang'];
     	if($_REQUEST['step']<$rownum)
     	{
     		sendEmail($email_to,$first_name,$url,$lang);
     	}
     	if($_REQUEST['step']==$rownum)
     	{
     		sendEmail1($email_to,$first_name,$url,$lang);
     	}
     	$arr=array('event_user_id'=>$event_customer_id,'step'=>$step,'allstep'=>$rownum);
		echo json_encode($arr);
	
	}
	
	if($_POST['method']=='getEvent')
	{
		$event_user_id=$_POST['event_user_id'];
		$sqlselect="select * from zy_event_customer where customer_id=".$event_user_id;
		$rs = mysql_query($sqlselect);
		$row = mysql_fetch_array($rs);
		$step=(int)$row['step']+1;
		$event=array('event_id'=>$row['event_id'],'step'=>$step,'event_user_id'=>$event_user_id,'emailaddress'=>$row['emailaddress'],'firstname'=>$row['firstname'],'mobile'=>$row['mobile']);
		echo json_encode($event);
	}
	
	
	if($_REQUEST['method']=="checkEmail")
	{
		$sql="select customer_id from zy_event_customer where emailaddress='".$_REQUEST['emailaddress']."' and event_id=".$_REQUEST['event_id']."";
		$result=mysql_query($sql);
	if(mysql_num_rows($result)==0){
		//email不存在
		echo "success";
	}else{
		
		$a=mysql_fetch_array($result);
		echo $a['customer_id'];
	}
	}
	
	
	//发送邮件
	function sendEmail($email_to,$first_name,$url,$lang){
		if($lang=="en")
		{
			$email_message =  file_get_contents("http://prepaidselect.com/demo1/newsletteren.html");
		}else{
			$email_message =  file_get_contents("http://prepaidselect.com/demo1/newsletter.html");
		}

	$email_message=str_replace("{first_name}",$first_name,$email_message);
	$email_message=str_replace("{url}",$url,$email_message);
	$email_subject  = "Netcube - 请确认注册领取您的1GB免费无线上网流量。";
	$headers = 'Content-type: text/html; charset=utf-8' . "\r\n";
	//$headers .= 'To: '.$email_to . "\r\n";
	$headers .= 'From: Netcube<sales@netcube.com.au>' . "\r\n";
	$headers .= "Reply-To: ". strip_tags('sales@netcube.com.au') . "\r\n";
	$headers .= 'X-Mailer: PHP/' . phpversion() . "\r\n";
	$i=@mail($email_to, $email_subject, $email_message, $headers); 
	}
	
	//发送邮件
	function sendEmail1($email_to,$first_name,$url,$lang){
		if($lang=="en")
		{
			$email_message =  file_get_contents("http://prepaidselect.com/demo1/newsletteren1.html");
		}else{
			$email_message =  file_get_contents("http://prepaidselect.com/demo1/newsletter1.html");
		}

	$email_message=str_replace("{first_name}",$first_name,$email_message);
	//$email_message=str_replace("{url}",$url,$email_message);
	$email_subject  = "Netcube - 请确认注册领取您的1GB免费无线上网流量。";
	$headers = 'Content-type: text/html; charset=utf-8' . "\r\n";
	//$headers .= 'To: '.$email_to . "\r\n";
	$headers .= 'From: Netcube<sales@netcube.com.au>' . "\r\n";
	$headers .= "Reply-To: ". strip_tags('sales@netcube.com.au') . "\r\n";
	$headers .= 'X-Mailer: PHP/' . phpversion() . "\r\n";
	$i=@mail($email_to, $email_subject, $email_message, $headers); 
	}
	
	//发送短信
	function sendSms($post_data)
	{
		$url = "http://sms.webnova.com.au/api/webnova_sms_api_lib.php";
				
				$ch = curl_init();
				
				curl_setopt($ch,CURLOPT_URL,$url );  
				
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);  
				   
				curl_setopt($ch,CURLOPT_FOLLOWLOCATION,0); 
				
				curl_setopt($ch, CURLOPT_POST, 1);
				
				// 把post的变量加上
				curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
				
				$output = curl_exec($ch);
				
				//调试使用
				if ($output === FALSE) {
					
				    $output="cURL Error: " . curl_error($ch);
				    
				}
				curl_close($ch);
	}
?>
